home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: lion.cs.latrobe.edu.au!boylesgj
- From: boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles)
- Subject: Class constructor usage within another class constructor problem!
- X-Nntp-Posting-Host: lion.cs.latrobe.edu.au
- Message-ID: <DoEv3x.IDL@latcs1.lat.oz.au>
- Sender: news@latcs1.lat.oz.au (news)
- Organization: Comp.Sci & Comp.Eng, La Trobe Uni, Australia
- Date: Sun, 17 Mar 1996 12:09:33 GMT
-
- See astericks for problems.
-
- // address.cpp
-
- #include "address.h"
-
- // Constructor
- Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
- {
- Number=ANumber;
-
- // *******************************************************************
- // The next two lines create objects of class String. Do these objects
- // get placed into the Street and City members of class Address, i.e.
- // are these lines equivelent to Address.Street=AStreet etc or have I
- // got it wrong?
- // *******************************************************************
- String Street(AStreet);
- String City(ACity);
- Zip=AZip;
- }
-
- // Copy constructor
- Address::Address(Address& AnAddress)
- {
- Number=AnAddress.Number;
-
- // **********************************************************************
- // Can't pass AnAddress.Street by reference i.e. AnAddress.Street & as
- // it generates a compile error. Why?
- // **********************************************************************
- String Street(AnAddress.Street);
-
- // *******************************************************************
- // Can't pass AnAddress.City by reference i.e. AnAddress.City &. Why?
- // *******************************************************************
- String City(AnAddress.City);
- Zip=AnAddress.Zip;
- }
-
- // Deconstructor
- Address::~Address()
- {
- Number=0;
- String Street("");
- String City("");
- Zip=0;
- }
-
- // address.h
-
- #ifndef __ADDRESS_H
- #define __ADDRESS_H
- #define STR_STREET " street"
-
- #include "strclass.h"
- #include "defines.h"
-
- class Address
- {
- private : int Number;
- String Street;
- String City;
- int Zip;
-
- public : // Constructors
- Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
- Address();
- // Copy constructor
- Address(Address& AnAddress);
- // Deconstructor
- ~Address();
- }
-
- #endif
-
- // strclass.cpp
-
- #include "strclass.h"
- #include "string.h"
-
- // Constructor
- String::String(const char *AString)
- {
- Length=strlen(AString)+1;
- TheString=new char[Length];
- strcpy(TheString,AString);
- }
-
- // Copy constructor
- String::String(const String& SourceString)
- {
- Length=SourceString.Length;
- TheString=new char[Length];
- strcpy(TheString,SourceString.TheString);
- }
-
- // Deconstructor
- String::~String()
- {
- delete TheString;
- }
-
- // strclass.h
-
- #ifndef __STRCLASS_H
- #define __STRCLASS_H
-
- #include <iostream.h>
- #include "defines.h"
-
- class String
- {
- int Length;
- char *TheString;
-
- public : // Constructor
- String(const char *AString="");
- // Copy constructor
- String(const String&);
- // Destructor
- ~String();
-
- };
-
- #endif
-
-